#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <queue>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifndef TEST
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> piii;

// fast input
inline ll read() {
    ll X = 0, w = 0;
    char ch = 0;
    while (!isdigit(ch)) {
        w |= ch == '-';
        ch = getchar();
    }
    while (isdigit(ch))
        X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}

// fast output
inline void write(ll x) {
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

#define pb push_back
#define mp make_pair

#define ip first.first
#define bn first.second
#define nip second

int parse_ip(string s) {
    int res = 0;
    int n = s.length();
    int tmp = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '.') {
            res <<= 8;
            res += tmp;
            tmp = 0;
        } else {
            tmp *= 10;
            tmp += s[i] - '0';
        }
    }
    res <<= 8;
    res += tmp;
    return res;
}

pii parse_cidr(string s) {
    int res = 0;
    int n = s.length();
    int tmp = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] == '.') {
            res <<= 8;
            res += tmp;
            tmp = 0;
        } else if (s[i] == '/') {
            res <<= 8;
            res += tmp;
            tmp = 0;
        } else {
            tmp *= 10;
            tmp += s[i] - '0';
        }
    }
    return {res, tmp};
}

bool match(int ipi, pii cidr) {
    int bc = cidr.second;
    return ((ipi ^ cidr.first) & ~((1ll << (32 - bc)) - 1)) == 0;
}

void output_ip(int ipi) {
    int msk = 255;
    write(ipi >> 24 & msk);
    putchar('.');
    write((ipi >> 16) & msk);
    putchar('.');
    write((ipi >> 8) & msk);
    putchar('.');
    write((ipi) & msk);
    putchar('\n');
}

void solve() {
    string ss, ts;
    cin >> ss >> ts;
    int s = parse_ip(ss), t = parse_ip(ts);
    int n = read();
    unordered_map<int, int> ip_to_pos;
    vector<int> vip;
    vector<vector<piii>> nxt(n);
    for (int i = 0; i < n; i++) {
        string ips;
        cin >> ips;
        vip.pb(parse_ip(ips));
        ip_to_pos[parse_ip(ips)] = i;
        int k = read();
        for (int j = 0; j < k; j++) {
            string cidrs, nip;
            cin >> cidrs >> nip;
            nxt[i].pb({parse_cidr(cidrs), parse_ip(nip)});
        }
    }
    vector<int> ans;
    ans.pb(ip_to_pos[s]);
    int now = ip_to_pos[s];
    while (now != ip_to_pos[t]) {
        int nxti = 0;
        int nxtlen = -1;
        for (auto p : nxt[now]) {
            if (match(t, {p.ip, p.bn})) {
                if (p.bn > nxtlen) {
                    nxti = ip_to_pos[p.nip];
                    nxtlen = p.bn;
                }
            }
        }
        ans.pb(nxti);
        now = nxti;
    }
    write(ans.size());
    putchar('\n');
    for (auto i : ans) {
        output_ip(vip[i]);
    }
}

int32_t main() {
    int t = 1;
    while (t--) {
        solve();
    }
}